package com.example.module_mine.diyView



import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import com.example.module_mine.R


/**
 * @author yaotianxue
 * @description 组合view
 * @date 2025/5/15
 */
class VerifyCodeItemView: FrameLayout{
    private var cursor: View
    private var indicatorView: View
    private var numberView: TextView

    private var cursorColor: Int
    private val indicatorColor: Int
    private val indicatorFocusColor: Int
    private val numberSize: Float

    private var alphaAnimator: ValueAnimator? = null




    constructor(context: Context):this(context,null)
    constructor(context: Context,attrs: AttributeSet?):this(context,attrs,0)
    constructor(context: Context,attrs: AttributeSet?,defStyleAttr: Int):this(context,attrs,defStyleAttr,0)
    constructor(context: Context,attrs: AttributeSet?,defStyleAttr: Int,defStyleRes: Int):super(context,attrs,defStyleAttr,defStyleRes){
        val parentView = LayoutInflater.from(context).inflate(R.layout.layout_verify_code_item, this)
        cursor = parentView.findViewById(R.id.view_cursor)
        indicatorView = parentView.findViewById(R.id.view_focus)
        numberView = findViewById(R.id.tv_number)
        //自定义属性
        val ty = context.obtainStyledAttributes(attrs, R.styleable.VerifyCodeItemView)
        cursorColor = ty.getColor(R.styleable.VerifyCodeItemView_cursorColor, Color.BLACK)
        indicatorColor = ty.getColor(R.styleable.VerifyCodeItemView_indicatorColor, Color.GRAY)
        indicatorFocusColor = ty.getColor(R.styleable.VerifyCodeItemView_indicatorFocusedColor, Color.BLACK)
        numberSize = ty.getDimension(R.styleable.VerifyCodeItemView_numberSize,
            (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 25f, context.resources.displayMetrics)))
        ty.recycle()
        //初始样式
        cursor.visibility = View.GONE
        cursor.setBackgroundColor(cursorColor)
        numberView.paint.textSize = numberSize
        indicatorView.setBackgroundColor(indicatorColor)

    }

    fun setNumber(number: String) {
        numberView.text = number
    }

    fun setFocus(focus: Boolean) {
        if (focus) {
            onFocused()
        } else {
            onUnfocused()
        }
    }

    private fun onFocused() {
        indicatorView.setBackgroundColor(indicatorFocusColor)
        cursor.visibility = View.VISIBLE
        alphaAnimator = ObjectAnimator.ofFloat(cursor, "alpha", 0f, 1f)
        alphaAnimator!!.duration = 1000
        alphaAnimator!!.repeatMode = ValueAnimator.RESTART
        alphaAnimator!!.repeatCount = -1
        alphaAnimator!!.start()

    }
    private fun onUnfocused() {
        indicatorView.setBackgroundColor(indicatorColor)
        cursor.visibility = View.GONE
        if (alphaAnimator != null) {
            alphaAnimator!!.cancel()
            alphaAnimator = null
        }
    }
}